package com.tedu.project_jt.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.tedu.project_jt.mapper.UserMapper;
import com.tedu.project_jt.pojo.User;
import com.tedu.project_jt.pojo.UserExample;

@RestController
public class TestController {
	//控制层直接调用数据库
	
	//从spring的ioc容器中取UserMapper接中的代理类的对象
	@Autowired
	UserMapper userMapper;
	@RequestMapping("/asc")
	public List<User> asc(){
		//让框架生成 order by user_id asc
		UserExample userExample=new UserExample();
		userExample.setOrderByClause("user_id asc");
		List<User> list=userMapper
				.selectByExample(userExample);
		return list;
	}
	//关闭服务器，tomcat started on port
	//到console看一下生成的sql
	@RequestMapping("/desc")
	public List<User> desc(){
		//让框架生成 order by user_id desc
		UserExample userExample=new UserExample();
		userExample.setOrderByClause("user_id desc");
		List<User> list=userMapper
				.selectByExample(userExample);
		return list;
	}
	@RequestMapping("/login")
	// localhost:8080/login?username=admin&password=admin
	public User login(String username,
			String password) {
		//生成where username=admin and password=admin
		//需要criteria,criteria是exmaple中的内部类
		UserExample userExample=new UserExample();
		//类名.内部类的类名
		//or()返回的是creteria对象
		UserExample.Criteria criteria=userExample.or();
		//生成 username=admin
		criteria.andUsernameEqualTo(username);
		criteria.andPasswordEqualTo(password);
		List<User> list=userMapper
				.selectByExample(userExample);
		return list.get(0);
	}
	//添加
	@RequestMapping("/insert")
	//  /insert?username=a&password=1
	public Integer insert(String username,
			String password) {
		//创建对象
		User user=new User();
		user.setUsername(username);
		user.setPassword(password);
		//不用设置userId
		//把对象传给mybatis框架，框架生成insert语句
		int count=userMapper.insert(user);
		return count;//添加的行数 >=1添加成功。
		
	}
	//修改
	@RequestMapping("/update")
	public Integer update(
			Integer userId,
			String username,
			String password
			) {
		//创建对象
		User user=new User();
		user.setUserId(userId);
		user.setUsername(username);
		user.setPassword(password);
		//调用userMapper接口的updateByPrimaryKey()
		//update user where user_id=1
		int count=userMapper.updateByPrimaryKey(user);
		return count;
	}
	
	@RequestMapping("/delete")
	//   /delete?userId=1
	// console生成的sql,sqlyog看user_id=1还在不在
	public int delete(Integer userId) {
		Integer count=userMapper.deleteByPrimaryKey(userId);
	
		return count;
	}
	
	//修改密码
	@RequestMapping("/changePassword")
	public Integer changePassword(
			String oldUsername,
			String oldPassword,
			String newPassword
			) {
		//创建where,创建userExample,criteria
		UserExample userExample=new UserExample();
		UserExample.Criteria criteria=userExample.or();
		criteria.andUsernameEqualTo(oldUsername);
		criteria.andPasswordEqualTo(oldPassword);
		//创建对象
		User user=new User();
		user.setPassword(newPassword);
		
		//update set user_id=,username=
		//user中只有password		
//		int count=userMapper
//				.updateByExample(user, userExample);
		//如果user对象中属性的值不为null,才会出现在set 列中
		//set password= where
		int count=userMapper
				.updateByExampleSelective(user, userExample);
		//console看一下生成的sql, sqlyog username的值是什么
		return count;
	}
}





